package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DlldrInfoMapper;
import com.ruoyi.system.domain.DlldrInfo;
import com.ruoyi.system.service.IDlldrInfoService;

/**
 * 设备采集配置Service业务层处理
 * 
 * @author yaozh
 * @date 2024-02-22
 */
@Service
public class DlldrInfoServiceImpl implements IDlldrInfoService 
{
    private static final Logger log = LoggerFactory.getLogger(DlldrInfoServiceImpl.class);

    @Autowired
    private DlldrInfoMapper dlldrInfoMapper;

    @Override
    public List<DlldrInfo> findAllDllDrInfo(String dBname){
        List<DlldrInfo>list=dlldrInfoMapper.findAllDllDrInfo(dBname);
        return list;
    }

    /**
     * 查询设备采集配置
     * 
     * @param drid 设备采集配置主键
     * @return 设备采集配置
     */
    @Override
    public DlldrInfo selectDlldrInfoByDrid(Long drid,String dbName)
    {
        return dlldrInfoMapper.selectDlldrInfoByDrid(drid,dbName);
    }

    /**
     * 查询设备采集配置列表
     * 
     * @param dlldrInfo 设备采集配置
     * @return 设备采集配置
     */
    @Override
    public List<DlldrInfo> selectDlldrInfoList(DlldrInfo dlldrInfo,String dbName)
    {
        return dlldrInfoMapper.selectDlldrInfoList(dlldrInfo,dbName);
    }

    /**
     * 新增设备采集配置
     * 
     * @param dlldrInfo 设备采集配置
     * @return 结果
     */
    @Override
    public int insertDlldrInfo(DlldrInfo dlldrInfo,String dbName)
    {
        return dlldrInfoMapper.insertDlldrInfo(dlldrInfo,dbName);
    }

    /**
     * 修改设备采集配置
     * 
     * @param dlldrInfo 设备采集配置
     * @return 结果
     */
    @Override
    public int updateDlldrInfo(DlldrInfo dlldrInfo,String dbName)
    {
        return dlldrInfoMapper.updateDlldrInfo(dlldrInfo,dbName);
    }

    /**
     * 批量删除设备采集配置
     * 
     * @param drids 需要删除的设备采集配置主键
     * @return 结果
     */
    @Override
    public int deleteDlldrInfoByDrids(Long[] drids,String dbName)
    {
        return dlldrInfoMapper.deleteDlldrInfoByDrids(drids,dbName);
    }

    /**
     * 删除设备采集配置信息
     * 
     * @param drid 设备采集配置主键
     * @return 结果
     */
    @Override
    public int deleteDlldrInfoByDrid(Long drid,String dbName)
    {
        return dlldrInfoMapper.deleteDlldrInfoByDrid(drid,dbName);
    }

    @Override
    public String importDlldrInfo(List<DlldrInfo> dllDrInfoList, Boolean isUpdateSupport, String operName,String dbName){
        if (StringUtils.isNull(dllDrInfoList) || dllDrInfoList.size() == 0)
        {
            throw new ServiceException("导入项目网关配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DlldrInfo dlldrInfo : dllDrInfoList)
        {
            try{
                // 验证是否存在这个用户
                DlldrInfo dll = dlldrInfoMapper.selectDlldrInfoByIpport(dlldrInfo.getIpport(),dbName);
                if (StringUtils.isNull(dll))
                {
                    dlldrInfo.setCreateBy(operName);
                    dlldrInfoMapper.insertDlldrInfo(dlldrInfo,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、IP和端口 " + dlldrInfo.getIpport() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    dlldrInfo.setDrid(dll.getDrid());
                    dlldrInfo.setUpdateBy(operName);
                    dlldrInfoMapper.updateDlldrInfo(dlldrInfo,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、IP和端口 " + dlldrInfo.getIpport() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、IP和端口 " + dlldrInfo.getIpport() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、接口名 " + dlldrInfo.getDrname() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
